<template>
    <view class="content">
        <lsj-edit
                ref="lsjEdit"
                :maxCount="200"
                :html="html"
                @onReady="editReady">
            <template v-slot:btns="data">
                <edit-btns :edit="data.edit" @submit="save"></edit-btns>
            </template>
        </lsj-edit>
    </view>
</template>

<script>
    import config from '@/config'
    import editBtns from '@/uni_modules/lsj-edit/components/lsj-edit/edit-btns/edit-btns';
    const baseUrl = config.baseUrl
    const imgUrl = config.imgUrl
    export default {
        emits:["getEditorText"],
        components:{
            // 富文本基本操作按键，可自行选择是否使用示例的按键
            editBtns
        },
        props:{
            html:{
                type:String,
                defaults:""
            }
        },
        data() {
            return {
                edit: null,
            }
        },
        onLoad() {

        },
        methods: {
            // 编辑器初始化完毕，返回edit对象
            editReady(edit) {
                // 将富文本对象存放到当前页面，便于后续直接操作
                this.edit = edit;
                console.log(this.edit)
            },
            // 演示发布
            async save() {
                // 获取插入的图片列表
                let imgs = await this.edit.getImages();
                // 判断是否允许提交
                if (!this.edit.textCount && !imgs.length) {
                    uni.showToast({
                        title: '请录入内容'
                    });
                }
                let aa="";
                // 将所有未上传的本地图片上传到服务器并替换到编辑器
                this.edit.replaceImage(async(img)=>{
                    if(img.indexOf('http') === 0) {return img;}
                    let url = await uni.uploadFile({
                        // url: baseUrl + '/upload/uploadPic',
                        url: baseUrl + '/circle/uploadImage?deviceId='+this.$store.state.user.deviceId,
                        filePath: img,
                        name: 'file',
                    });
                    return imgUrl+JSON.parse(url[1].data)["src"]
                }).then(res=>{
                    this.$emit('getEditorText',res.html);
                });

            },
        }
    }
</script>

<style scoped lang="scss">
</style>
